home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include "global.h"
- #include "mbuf.h"
- #include "socket.h"
- #include "proc.h"
- #include "ftp.h"
- #include "ftpcli.h"
-
- extern int16 Tcp_mss;
-
- /* Send a file (opened by caller) on a network socket */
- long
- sendfile(fp,s,mode,hash)
- FILE *fp; /* File to be sent */
- int s; /* Socket to be sent on */
- int mode; /* Transfer mode */
- int hash; /* Print hash marks every Tcp_mss bytes */
- {
- register struct mbuf *bp;
- int c,oldf;
- long total = 0;
- long hmark = 0;
-
- if(hash == V_BYTE)
- tprintf(" : Bytes Sent\r");
- switch(mode){
- default:
- case LOGICAL_TYPE:
- case IMAGE_TYPE:
- for(;;){
- if((bp = alloc_mbuf(BLKSIZE)) == NULLBUF)
- return -1;
-
- if((bp->cnt = fread(bp->data,1,BLKSIZE,fp)) == 0){
- free_p(bp);
- break;
- }
- total += bp->cnt;
- if(send_mbuf(s,bp,0,NULLCHAR,0) == -1){
- return -1;
- }
- while(hash == V_HASH && total >= hmark+1000){
- tputc('#');
- hmark += 1000;
- }
- while(hash == V_BYTE && total >= hmark+Tcp_mss){
- tprintf("%8ld\r", total);
- hmark += Tcp_mss;
- }
-
- }
- break;
- case ASCII_TYPE:
- oldf = setflush(s,-1);
- /* Let the newline mapping code in usputc() do the work */
- while((c = fgetc(fp)) != EOF){
- #if !defined(UNIX) && !defined(__TURBOC__) && !defined(__TURBOC__)
- if(c == '\r'){
- /* Needed only if the OS uses a CR/LF
- * convention and fgetc doesn't do
- * an automatic translation
- */
- continue;
- }
- #endif
- usputc(s,(char)c);
- total++;
- while(hash == V_HASH && total >= hmark+1000){
- tputc('#');
- hmark += 1000;
- }
- while(hash == V_BYTE && total >= hmark+Tcp_mss){
- tprintf("%8ld\r", total);
- hmark += Tcp_mss;
- }
- }
- usflush(s);
- setflush(s,oldf);
- break;
- }
- return total;
- }
- long
- recvfile(fp,s,mode,hash)
- FILE *fp;
- int s;
- int mode;
- int hash;
- {
- int cnt,c;
- struct mbuf *bp;
- long total = 0;
- long hmark = 0;
-
- if(hash == V_BYTE)
- tprintf(" : Bytes Received\r");
- switch(mode){
- default:
- case LOGICAL_TYPE:
- case IMAGE_TYPE:
- while((cnt = recv_mbuf(s,&bp,0,NULLCHAR,0)) != 0){
- if(cnt == -1)
- return -1;
-
- total += cnt;
- while(hash == V_HASH && total >= hmark+1000){
- tputc('#');
- hmark += 1000;
- }
- while(hash == V_BYTE && total >= hmark+Tcp_mss){
- tprintf("%8ld\r", total);
- hmark += Tcp_mss;
- }
- if(fp != NULLFILE){
- if(write_p(fp,bp) == -1){
- free_p(bp);
- return -1;
- }
- free_p(bp);
- } else {
- send_mbuf(Curproc->output, bp, 0, NULLCHAR, 0);
- }
- }
- break;
- case ASCII_TYPE:
- while((c = recvchar(s)) != EOF){
- if(fp != NULLFILE){
- #if !defined(UNIX) && !defined(__TURBOC__) && !defined(AMIGA)
- if(c == '\n'){
- /* Needed only if the OS uses a CR/LF
- * convention and fputc doesn't do
- * an automatic translation
- */
- fputc('\r',fp);
- }
- #endif
- if(fputc(c,fp) == EOF){
- total = -1;
- break;
- }
- } else {
- tputc((char)c);
- }
- total++;
- while(hash == V_HASH && total >= hmark+1000){
- tputc('#');
- hmark += 1000;
- }
- while(hash == V_BYTE && total >= hmark+Tcp_mss){
- tprintf("%8ld\r", total);
- hmark += Tcp_mss;
- }
- }
- break;
- }
- return total;
- }
-
- /* Determine if a file appears to be binary (i.e., non-text).
- * Return 1 if binary, 0 if ascii text after rewinding the file pointer.
- *
- * Used by FTP to warn users when transferring a binary file in text mode.
- */
- int
- isbinary(fp)
- FILE *fp;
- {
- int c,i;
- int rval;
-
- rval = 0;
- for(i=0;i<512;i++){
- if((c = getc(fp)) == EOF)
- break;
- if(c & 0x80){
- /* High bit is set, probably not text */
- rval = 1;
- break;
- }
- }
- /* Assume it was at beginning */
- fseek(fp,0L,0);
- return rval;
- }
-